package com.hl.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hl.domain.Specification;
import com.hl.service.ISpecificationService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 规格列表Controller
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@RestController
@RequestMapping("/specification")
public class SpecificationController extends BaseController
{
    @Autowired
    private ISpecificationService specificationService;

    /**
     * 查询规格列表列表
     */
    @RequiresPermissions("shangpin:specification:list")
    @GetMapping("/list")
    public TableDataInfo list(Specification specification)
    {
        startPage();
        List<Specification> list = specificationService.selectSpecificationList(specification);
        return getDataTable(list);
    }

    /**
     * 导出规格列表列表
     */
    @RequiresPermissions("shangpin:specification:export")
    @Log(title = "规格列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Specification specification)
    {
        List<Specification> list = specificationService.selectSpecificationList(specification);
        ExcelUtil<Specification> util = new ExcelUtil<Specification>(Specification.class);
        util.exportExcel(response, list, "规格列表数据");
    }

    /**
     * 获取规格值列表
     */
    @GetMapping("/values")
    public AjaxResult getSpecificationValues()
    {
        List<Specification> list = specificationService.selectSpecificationList(new Specification());
        // 去重获取所有规格值
        List<String> uniqueValues = list.stream()
            .map(Specification::getSpecificationValue)
            .filter(value -> value != null && !value.trim().isEmpty())
            .distinct()
            .collect(java.util.stream.Collectors.toList());
        
        System.out.println("去重后的规格值列表大小: " + uniqueValues.size());
        for (String value : uniqueValues) {
            System.out.println("规格值: " + value);
        }
        return success(uniqueValues);
    }

    /**
     * 获取规格列表详细信息
     */
    @RequiresPermissions("shangpin:specification:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(specificationService.selectSpecificationById(id));
    }

    /**
     * 新增规格列表
     */
    @RequiresPermissions("shangpin:specification:add")
    @Log(title = "规格列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Specification specification)
    {
        return toAjax(specificationService.insertSpecification(specification));
    }

    /**
     * 修改规格列表
     */
    @RequiresPermissions("shangpin:specification:edit")
    @Log(title = "规格列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Specification specification)
    {
        return toAjax(specificationService.updateSpecification(specification));
    }

    /**
     * 删除规格列表
     */
    @RequiresPermissions("shangpin:specification:remove")
    @Log(title = "规格列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(specificationService.deleteSpecificationByIds(ids));
    }
}
